﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using NHibernate;
using hbse.Entities;
using hbse.Service;

namespace hbse
{
    public partial class Ussimplecustomer : UserControl
    {
        private TabItem newTab;
        private DevComponents.DotNetBar.TabControl tabControl2;
        private ISession session;
        private Users users;
        private UntilService untilService = new UntilService();
        private Customer customer;
        private SampleCustomerService sampleCustomerService = new SampleCustomerService();

        private BarTender.ApplicationClass btApp;
        private BarTender.Format btformat;

        public Ussimplecustomer(DevComponents.DotNetBar.TabControl _tabControl2, TabItem _newTab, ISession _session,Users _users)
        {
            InitializeComponent();
            newTab = _newTab;
            tabControl2 = _tabControl2;
            session = _session;
            users = _users;

            try
            {
                btApp = new BarTender.ApplicationClass();
            }
            catch (Exception ex)
            {

                throw ex;
            }
        }

         

        private void Ussimplecustomer_Load(object sender, EventArgs e)
        {
            
        }

        private void button2_Click(object sender, EventArgs e)
        {
             
            
        }

        private void button1_Click(object sender, EventArgs e)
        {
            List<hbse.DTO.CustomerDTO> customerDTOs = untilService.getCustomersByCode(txtsearch.Text.Trim(), session);
            if (customerDTOs.Count > 0)
            {
                dgvCustomer.DataSource = customerDTOs;
                FormatdgvCustomer();
            }
            else
            {
                dgvCustomer.DataSource = null;


            }
        }

        private void FormatdgvCustomer()
        {
            if (dgvCustomer.RowCount > 0)
            {
                dgvCustomer.Columns["customerCode"].HeaderText = "customerCode";
                dgvCustomer.Columns["customerName"].HeaderText = "customerName";

                dgvCustomer.Columns["Id"].Visible = false;

            }
        }

        private void dgvCustomer_CellMouseUp(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.RowIndex == -1)
            {
                return;
            }

            customer = untilService.getCustomerbyCode(dgvCustomer.Rows[e.RowIndex].Cells["customerCode"].Value.ToString(),session);
            if (customer != null)
            {
                txtCode.Text = customer.CustomerCode;
                txtName.Text = customer.CustomerName;

            }
            else {
                txtCode.Text = "";
                txtName.Text = "";
            }

        }

        private void cmdprint_Click(object sender, EventArgs e)
        {
            if (txtCode.Text.Trim()=="")
            {
                DevComponents.DotNetBar.MessageBoxEx.Show("กรุณาเลือกข้อมูลก่อน !!!", "ผลการตรวจสอบ", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                dgvCustomer.Focus();
                return;
            }
            if (txtnumPrint.Text.Trim() == "")
            {
                DevComponents.DotNetBar.MessageBoxEx.Show("กรุณาระบุจำนวนก่อน !!!", "ผลการตรวจสอบ", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                txtnumPrint.Focus();
                return;
            }

            List<CustomerPrint> _customerPrints = new List<CustomerPrint>();
            CustomerPrint customerPrint;
            int count = Convert.ToInt32(txtnumPrint.Text.Trim());
            for (int i = 0; i < count; i++)
            {
                customerPrint = new CustomerPrint();
                customerPrint.Users = users;
                customerPrint.Customer = customer;
                customerPrint.NumPrint = 1;
                customerPrint.PrintDate = DateTime.Now;
                _customerPrints.Add(customerPrint);

                try
                {

                    string path = Application.StartupPath + "\\SampleFolderCustomer.btw";

                    btApp.Visible = false;
                    btformat = btApp.Formats.Open(path, true, "");

                    // barcode 
                    btformat.SetNamedSubStringValue("name", customer.CustomerName); // 1.Barcode
             
                    btformat.PrintOut(false, false);

                    //print barcode

                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex);
                }
                finally
                {
                    btformat.Close(BarTender.BtSaveOptions.btDoNotSaveChanges);
                }   

            }
            customer.CustomerPrints.Clear();
            customer.CustomerPrints = _customerPrints;

            int result = sampleCustomerService.updateCustomer(customer, session);
            if (result > -1)
            {
                Console.WriteLine("Update Complete");
                MessageBox.Show("บันทึกข้อมูล เรียบร้อยแล้ว !!!", "ผลการทำงาน", MessageBoxButtons.OK, MessageBoxIcon.Information);


            }
            else
            {
                Console.WriteLine("Update Not Complete");
                MessageBox.Show("ไม่สามารถบันทึกข้อมูลได้ !!!", "ผลการทำงาน", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }

        }

       
    }
}
